<template>
	<div class="sendNotice">
		<div class="classIndex_lk classIndex_h">
			<router-link :to="{path: '/class/resource/index', query: {id: path}}">班级资源</router-link>
			<strong><i>></i>编辑</strong>
		</div>
		<div class="sendNotice_form">
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				<!-- <el-form-item label="上传资源" prop="type">
					<el-checkbox-group v-model="ruleForm.type">
						<div class="r_fileBox"><span class="r_file box">选择资源</span></div>
					</el-checkbox-group>
				</el-form-item> -->
				<el-form-item label="权限" prop="authority">
					<el-radio-group v-model="ruleForm.authority">
						<el-radio :label="1">公开：同步到平台资源/视频</el-radio>
						<el-radio :label="0">不公开：仅限班级使用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="资源名称" prop="name">
					<el-input v-model.trim="ruleForm.name" placeholder="50个字符内" maxlength="50"></el-input>
				</el-form-item>

				<el-form-item label="科目" prop="subject_id" v-if="0">
					<el-select filterable v-model="ruleForm.subject_id" placeholder="请选择科目">
						<el-option 
						v-for="(item, index) in orgSubject"
						:label="item.name" :value="item.id" :key="index"></el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="课程简介" prop="profile" v-if="0">
					<el-input type="textarea" v-model.trim="ruleForm.profile"
					:rows="6"
					maxlength="200"
					resize="none"
					placeholder="请输入课程简介描述"></el-input>
					<span class="sunWords"><i>{{ ruleForm.profile.length }}</i>/200</span>
				</el-form-item>
				<el-form-item class="sendNotice_btn buttonCSS">
					<el-button type="primary" plain @click="resetForm('ruleForm')">取消</el-button>
					<el-button type="primary" @click="submitForm('ruleForm')"  v-preventReClick>保存</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
	
	export default {
		data() {
			return {
				path: this.$route.query.id,
				orgSubject: this.$g.subjectList,
				ruleForm: {
					name: '',
					authority: '',
					subject_id: '',
					profile: ''
				},
				rules: {
					name: [
						{ required: true, message: '请输入资源名称', trigger: 'blur' },
						{ min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
					],
					authority: [
						{ required: true, message: '请选择', trigger: 'change' }
					],
					subject_id: [
						{ required: true, message: '请选择学段', trigger: 'change' }
					],
					profile: [
						{ required: true, message: '请填写活动形式', trigger: 'blur' }
					]
				}

			};
		},
		created(){
			this.getResourceList()
		},
		watch: {
			'changData'(val, newval){
				this.path = sessionStorage.getItem('classId')
				this.getResourceList()
			}
		},
		computed: {
			changData() {
				return deep_clone(this.$store.state.changData);
			},
		},
		methods: {
			saveResource(){
				let success = (res=>{
					if (this.roleBaseid(4)) {
						this.$message.success('上传成功，请等待教师审核');
					}else{
						this.$message.success(res.msg);
					}
					setTimeout( () => {
						this.$router.push({path: '/class/resource/index',query: {id: this.path}})
					}, 1000);
				})
				var params = {
						id: this.$route.query.rid,
						authority: this.ruleForm.authority,
						class_id:this.ruleForm.class_id,
						name: this.ruleForm.name,
						subject_id:  this.ruleForm.subject_id,
						profile: this.ruleForm.profile,
						is_download:1
				}
				this.$ajax.put('api/resource/vue_resource',params,{func:{success:success}})
				
			},
			getResourceList(){
				let success = (res=>{
					let data = res.data[0]
					this.ruleForm.name = data.name;
					this.ruleForm.class_id = data.class_id;
					this.ruleForm.subject_id = data.subject_id;
					this.ruleForm.authority = data.authority;
					this.ruleForm.profile  = data.profile;

				})
				this.$ajax.get('api/resource/resource',{params:{id: this.$route.query.rid},func:{success:success}})
				
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.saveResource();
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$router.back(-1)
			},

		}
	}
</script>